﻿namespace N2.Details
{
	public static class DetailCollectionExtensions
	{
		public static DetailCollectionDictionaryWrapper<T> AsDictionary<T>(this DetailCollection collection)
		{
			return new DetailCollectionDictionaryWrapper<T>(collection);
		}

		public static DetailCollectionListWrapper<T> AsList<T>(this DetailCollection collection)
		{
			return new DetailCollectionListWrapper<T>(collection);
		}

		public static bool Rename(this DetailCollection collection, string name)
		{
			if (string.Equals(collection.Name, name)) {
				return false;
			}

			var _detailsDictionary = collection.EnclosingItem.DetailCollections;
			_detailsDictionary.Remove(collection.Name);
			_detailsDictionary.Add(name, collection);

			return true;
		}
		
		public static int IndexOf(this DetailCollection collection, string key)
		{
			for (int i = 0; i < collection.Details.Count; i++)
				if (collection.Details[i].Name == key)
					return i;
			return -1;
		}
	}
}
